package com.xhs.hawaii.common.utils;

import com.xhs.hawaii.common.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author tangchuang
 * @version 1.0
 * @since 2017/12/15
 */
@Slf4j
public class AssertUtils {

    public static void notNull(Object data, String message, Object... objects) {
        if (Objects.isNull(data)) {
            log.error(message, objects);
            throw new BusinessException(beauty(message));
        }
    }

    public static void notBlank(String data, String message, Object... objects) {
        if (StringUtils.isBlank(data)) {
            log.error(message, objects);
            throw new BusinessException(beauty(message));
        }
    }

    public static void check(boolean expression, String message, Object... objects) {
        if (!expression) {
            log.error(message, objects);
            throw new BusinessException(beauty(message));
        }
    }

    private static String beauty(String message) {
        if (message == null) {
            return null;
        }
        return StringUtils.split(",")[0];
    }
}
